// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// s [author] rhyme_lph
// W [email]  rhymelph@gmail.com
// l [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/mixin/check_plan_mixin.dart';
import 'package:finance_app/src/provider/dashboard/sales/views/sales_invoice_provider.dart';
import 'package:finance_app/src/provider/dashboard/sales/views/sales_quotation_provider.dart';
import 'package:finance_app/src/provider/dashboard/sales/views/sales_summary_provider.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:finance_app/src/utils/permission_utils.dart';
import 'package:finance_app/src/widgets/bottom_sheet/dashboard_add_bottom_sheet.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class SalesProvider extends BaseProvider with CheckPlanMixin {
  List<String> get tabList => [
        S.current.summary,
        if (Config.hasPermission(PermissionUtils.quotation))
          S.current.quotation,
        if (Config.hasPermission(PermissionUtils.invoice)) S.current.invoice,
      ];

  bool buttonBarIsUp = true;

  String initFilter;

  void handleScrollUpdate(ScrollController controller) {
    if (controller.position.userScrollDirection == ScrollDirection.reverse &&
        buttonBarIsUp == true) {
      //向下滑动
      buttonBarIsUp = false;
      update();
    }
    if (controller.position.userScrollDirection == ScrollDirection.forward &&
        buttonBarIsUp == false) {
      buttonBarIsUp = true;
      update();
    }
  }

  @override
  Future get dataFuture => null;

  @override
  List<BaseProvider> registerChildProviders() => [
        SalesSummaryProvider(),
        SalesQuotationProvider(),
        SalesInvoiceProvider(),
      ];

  void onNavAdd() async {
    if (isPlanExpired()) return;
    if (tabController != null) {
      currentIndex =
          (tabController.index + tabController.offset).round().clamp(0, 100);
    }
    if (tabList[currentIndex] == S.current.summary) {
      obtainContext?.call((context) async {
        await showDashboardAddBottomSheet(context,
            dashTypes: [DashBoardAddType.saleSummary]);
        getChildProvider<SalesSummaryProvider>()?.onRefresh();
      });
    } else if (tabList[currentIndex] == S.current.quotation) {
      final result =
          await Routes.navigateTo(RouterGenProviders.newQuotationPage, params: {
        'isSelectMode': true,
      });
      if (result != null) {
        getChildProvider<SalesQuotationProvider>()
            .refreshController
            .requestRefresh();
      }
    } else if (tabList[currentIndex] == S.current.invoice) {
      final result =
          await Routes.navigateTo(RouterGenProviders.newInvoicePage, params: {
        'isSelectMode': true,
      });
      if (result != null) {
        getChildProvider<SalesInvoiceProvider>()
            .refreshController
            .requestRefresh();
      }
    }
  }

  int currentIndex = 0;

  TabController tabController;

  void onChangeTab(TabController controller) {
    currentIndex = (controller.index + controller.offset).round();
  }
}
